package com.ddtech.framework.notice.rest;

import com.ddtech.framework.log.aop.log.Log;
import com.ddtech.framework.notice.model.SysNotice;
import com.ddtech.framework.notice.service.SysNoticeService;
import com.ddtech.framework.notice.service.dto.SysNoticeQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @author bin_pu
* @date 2019-11-27
*/
@Api(tags = "系统-公告管理")
@RestController
@RequestMapping("api")
public class SysNoticeController {

    @Autowired
    private SysNoticeService sysNoticeService;

    @Log("查询公告列表")
    @ApiOperation(value = "查询公告列表")
    @GetMapping(value = "/sysNotice")
    public ResponseEntity getSysNotices(SysNoticeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(sysNoticeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增公告")
    @ApiOperation(value = "新增公告")
    @PostMapping(value = "/sysNotice")
    @PreAuthorize("hasAnyRole('ADMIN','SYSNOTICE_ALL','SYSNOTICE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody SysNotice resources){
        return new ResponseEntity(sysNoticeService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改公告")
    @ApiOperation(value = "修改公告")
    @PutMapping(value = "/sysNotice")
    @PreAuthorize("hasAnyRole('ADMIN','SYSNOTICE_ALL','SYSNOTICE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody SysNotice resources){
        sysNoticeService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除公告")
    @ApiOperation(value = "删除公告")
    @DeleteMapping(value = "/sysNotice/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','SYSNOTICE_ALL','SYSNOTICE_DELETE')")
    public ResponseEntity delete(@PathVariable String id){
        sysNoticeService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "查询公告ByID")
    @GetMapping(value = "/sysNotice/{id}")
    public ResponseEntity create(@PathVariable String id){
        return new ResponseEntity(sysNoticeService.findById(id),HttpStatus.CREATED);
    }
}